<?
if ($_SERVER['SERVER_NAME'] == 'localhost') {
    
    $path = '/';
}
elseif($_SERVER['SERVER_NAME'] == 'dev.gstestequipment.com') {
    
    $path = '/home/mikepaer_v1/dev.gstestequipment.com/';
}

require_once($path . 'includes_php/helpers/functions.php');

/* Prepare data for AJAX stuff */

if(!empty($_GET['type'])) {
    $typeId = $_GET['type'];
    $typeName = lookup_typeById($typeId);
}

if(!empty($_GET['brand'])) {
    $brandId = $_GET['brand'];
    $brandName = lookup_brandById($brandId);
}


$paginateBy = 50;

if (!empty($_GET['page'])) {
    if (is_numeric($_GET['page'])) {
	$start = $_GET['page'] * $paginateBy;;
    }
} else {
    $start = 0;
}

$sortRules = array();

if(!empty($_GET['sortBy'])) {
    $sortStr = $_GET['sortBy'];
    
    // String expected to be:   criteria__order--criteria__order

    $sortByArray = explode("--", $sortStr); // now sortByArray contains rules

    foreach($sortByArray as $rules) {
	// rule[0] is field
	// rule[1] is dsc/asc
	
	//Error checking
	if(!strstr($rules, "__")) {
	    exit(0);
	}
	$rule = explode("__", $rules);
	

	
	switch($rule[0]) {
	    case 'brand': $sortCriteria = gsProducts::FIELD_LK_BRAND; break;
	    case 'modelNo': $sortCriteria = gsProducts::FIELD_MODELNO; break;
	    case 'type': $sortCriteria = gsProducts::FIELD_LK_TYPE; break;
	    case 'price': $sortCriteria = gsProducts::FIELD_PRICE; break;
	    case 'shortDesc': $sortCriteria = gsProducts::FIELD_SHORTDESC; break;
	    default: break;
	}
	
	switch($rule[1]) {
	    case 'asc': $direction = DSC::ASC; break;
	    case 'desc': $direction = DSC::DESC; break;
	    default: break;
	}
	
	$sortRules[] = new DSC($sortCriteria, $direction);
    }
    
    // Now we have $sortRules, filled with the DSC rules and 
    // ready to be input into the function.
}
else {
    $sortStr = null;
}
?>

<script>
    $(function() {
	$( ".getAQuote" ).button({ icons: { primary: "ui-icon-tag" }});
	$( ".paginationButton" ).button();
	$( "li#active a ").button("option", "disabled", true);
		
    });
</script>

<?
$results = array();
$results = getProducts($typeId, $brandId, $sortRules);

$totalProducts = count($results);

/* Kill if no results. */
if($totalProducts < 1) {
    echo "No products to display.";
    exit(0);
}

$slicedArray = array_slice($results, $start, ($start + $paginateBy));

$ctr = 0;
?>

<div class="pagination">
    <? echo paginate($start / $paginateBy, $totalProducts, $paginateBy, $typeId, $brandId, $sortStr); ?>
</div>

<table id="dataTable">
    <thead>
	<tr>
	    <!--<th class="nobg"></th>-->
	    <th>Brand</th>
	    <th>Model</th>
	    <th>Description</th>
	    <th>Price</th>
	    <th>Quote</th>
	</tr>
    </thead>

    <tbody>
<?
foreach ($slicedArray as $ret) {
    echo "<tr class=\"";
    if ($ctr % 2 == 0) {
	echo "spec";
    } else {
	echo "specalt";
    }
    echo "\">";
    //echo "<td class=\"img\"><img src=\"images/equipment/MSOX3054A.jpg\" width=50px /></td>";
    echo "<td>" . $brandName . "</td>";
    echo "<td>" . $ret->getModelNo() . "</td>";
    echo "<td>" . $ret->getShortDesc() . "</td>";
    echo "<td>" . priceFormat($ret->getPrice()) . "</td>";
    echo "<td class=\"getAQuoteTd\"><a href=\"quote.php?id=" . $ret->getId() . "\" class=\"getAQuote\">Get A Quote</a></td>";
    echo "</tr>";
    $ctr++;
}
?>

    </tbody>

    <tfoot>
    </tfoot>
</table>

<div class="pagination">
    <? echo paginate($start / $paginateBy, $totalProducts, $paginateBy, $typeId, $brandId, $sortStr); ?>
</div>